#include "QQEmojiModel.h"

QQEmojiModel::QQEmojiModel(QObject* parent)
	: QAbstractListModel(parent)
{
	mMovie = new QMovie(this);
	mEmojis.setCapacity(100);
}

QQEmojiModel::~QQEmojiModel()
{
	for (int i = 0; i < mEmojis.size(); i++)
	{
		delete mEmojis.at(i);
	}
}

void QQEmojiModel::addEmoji(const QQChatEmoji& emoji)
{
	this->beginInsertRows(QModelIndex(), mEmojis.size(), mEmojis.size());
	mEmojis.append(new QQChatEmoji(emoji));
	this->endInsertRows();
}


void QQEmojiModel::addEmojis(const QList<QQChatEmoji>& emojis)
{
	this->beginInsertRows(QModelIndex(), mEmojis.size(), mEmojis.size() + emojis.size() - 1);
	for (const QQChatEmoji& emoji : emojis)
	{
		mEmojis.append(new QQChatEmoji(emoji));
	}
	this->endInsertRows();
}

QString QQEmojiModel::getEmojiPreview(long id)
{
	QQChatEmoji* emoji = nullptr;
	for (int i = 0; i < mEmojis.size(); i++)
	{
		if (mEmojis.at(i)->id == id)
		{
			emoji = mEmojis.at(i);
			break;
		}
	}
	if (emoji == nullptr)
	{
		return QString();
	}
	return emoji->preview;
}

int QQEmojiModel::getMovieFrameCount(long id)
{
	QQChatEmoji* emoji = nullptr;
	for (int i = 0; i < mEmojis.size(); i++)
	{
		if (mEmojis.at(i)->id == id)
		{
			emoji = mEmojis.at(i);
			break;
		}
	}
	if (emoji == nullptr)
	{
		return 0;
	}
	if (mCurrentPath != emoji->path)
	{
		mMovie->setFileName(emoji->path);
		mCurrentPath = emoji->path;
	}
	return mMovie->frameCount();
}

QImage QQEmojiModel::getMovieFrame(long id, int index, int width, int height)
{
	QQChatEmoji* emoji = nullptr;
	for (int i = 0; i < mEmojis.size(); i++)
	{
		if (mEmojis.at(i)->id == id)
		{
			emoji = mEmojis.at(i);
			break;
		}
	}
	if (emoji == nullptr)
	{
		return QImage();
	}
	if (mCurrentPath != emoji->path)
	{
		mMovie->setFileName(emoji->path);
		mCurrentPath = emoji->path;
	}
	mMovie->jumpToFrame(index % mMovie->frameCount());
	return mMovie->currentImage().scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}

int QQEmojiModel::getLottieFrameCount(long id)
{
	QQChatEmoji* emoji = nullptr;
	for (int i = 0; i < mEmojis.size(); i++)
	{
		if (mEmojis.at(i)->id == id)
		{
			emoji = mEmojis.at(i);
			break;
		}
	}
	if (emoji == nullptr)
	{
		return 0;
	}
	if (mCurrentPath != emoji->path)
	{
		mLottie.release();
		mLottie = rlottie::Animation::loadFromFile(emoji->path.toStdString().c_str());
		mCurrentPath = emoji->path;
	}
	return mLottie->totalFrame();
}

QImage QQEmojiModel::getLottieFrame(long id, int index, int width, int height)
{
	QQChatEmoji* emoji = nullptr;
	for (int i = 0; i < mEmojis.size(); i++)
	{
		if (mEmojis.at(i)->id == id)
		{
			emoji = mEmojis.at(i);
			break;
		}
	}
	if (emoji == nullptr)
	{
		return QImage();
	}
	if (mCurrentPath != emoji->path)
	{
		mLottie.release();
		mLottie = rlottie::Animation::loadFromFile(emoji->path.toStdString().c_str());
		mCurrentPath = emoji->path;
	}
	QImage image(width, height, QImage::Format_ARGB32);
	rlottie::Surface surface(reinterpret_cast<uint32_t*>(image.bits()), width, height, image.bytesPerLine());
	mLottie->renderSync(index % mLottie->totalFrame(), surface);
	return image;
}

int QQEmojiModel::rowCount(const QModelIndex& parent) const
{
	return mEmojis.count();
}

QVariant QQEmojiModel::data(const QModelIndex& index, int role) const
{
	if (role == Qt::UserRole)
	{
		return QVariant::fromValue(mEmojis.at(index.row()));
	}
	return QVariant();
}
